<template>
	<view class="content">
		<swiper class="swiper" circular @change="swiperChange" :current="swiperCurrent">
			<swiper-item v-for="(item,index) in imageIconAry" :key="index">
				<image class="img1" :src="item.imageUrl"></image>
			</swiper-item>
		</swiper>
		<text class="text1">{{textTime1}}</text>
		<text class="text2">{{textTime2}}</text>
		<view class="word">图片来源：{{creatyby==''?'网络':createBy}}</view>
		<swiper class="ad" autoplay="true" interval="3000" circular="true" :style="{'height':'400rpx' }">
			<swiper-item class="ad-item" v-if="bannerAdShow">
				<ad unit-id="adunit-32ee9f1ef2bea9fa" @error="offAd" @close="offAd"></ad>
			</swiper-item>
			<swiper-item class="ad-item" v-for="(item, index) in bannerList" :key="index" @click="goSwiperDetail(item)"
				v-if="item.status==0">
				<image class="ad-img" v-if="item" :src="item.img"></image>
			</swiper-item>
		</swiper>

		<view class="word">更多推荐</view>

		<wallpaper class="wallpapaer-class" :imgList="imageIconAry"></wallpaper>

		<view class="bottom-view f-row r-center c-between">
			<view class="view-share f-col r-center c-center">
				<img src="../../static/img/wallpaper/wechat.png" alt="">
				<text>发给好友</text>
				<button class="share-button" open-type="share"></button>
			</view>
			<view class="view-download f-col r-center c-center" @click="openAd">
				<text>下载原图</text>
			</view>
		</view>
	</view>
</template>

<script>
	import Wallpaper from '../../components/wallpaper/wallpaper.vue';
	export default {
		data() {
			return {
				bannerAdShow: true,
				imageUrl: "",
				textTime1: "",
				textTime2: "",
				creatyby: "",
				imageIconAry: [],
				imageId: 0,
				videoAd: null,
				bannerList: [],
				swiperCurrent: 0
			};
		},
		components: {
			Wallpaper
		},
		onLoad(item) {
			this.imageId = item.imageId;
			this.creatyby = item.creatyby
			this.getCurrTime();
			this.getImageList();
			if (wx.createRewardedVideoAd) {
				this.videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-07a137eb3c1a3917'
				})
				this.videoAd.onLoad(() => {})
				this.videoAd.onError((err) => {
					console.error('激励视频光告加载失败', err)
				})
				this.videoAd.onClose((res) => {
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						this.downloadImage()
					} else {
						// 播放中途退出，不下发游戏奖励
						console.log('广告中途关闭');
					}
				})
			}
			this.getBannerList();
		},
		methods: {
			offAd() {
				this.bannerAdShow = false
			},
			openAd() {
				// 用户触发广告后，显示激励视频广告
				if (this.videoAd) {
					this.videoAd.show().catch(() => {
						// 失败重试
						this.videoAd.load()
							.then(() => this.videoAd.show())
							.catch(err => {
								console.error('激励视频 广告显示失败', err)
							})
					})
				}
			},
			downloadImage() {
				let imageObject = this.imageIconAry[this.swiperCurrent];
				this.$api.request({
					url: this.$api.userdownsAdd,
					data: {
						userId: uni.getStorageSync('userId'),
						imageId: imageObject.id
					},
					method: "POST",
				}).then(res => {
					if (res.code == 200) {
						uni.downloadFile({
							url: imageObject.imageUrl,
							success: function(res) {
								if (res.statusCode === 200) {
									uni.saveImageToPhotosAlbum({
										filePath: res.tempFilePath,
										success: function() {
											uni.showToast({
												title: '图片下载成功',
												icon: 'none'
											})
										},
										fail: function(err) {
											console.log('保存图片到相册失败', err);
											uni.showToast({
												title: '图片下载失败',
												icon: 'none'
											})
										}
									});
								}
							},
							fail: function(err) {
								console.log('下载图片失败', err);
								uni.showToast({
									title: '图片下载失败',
									icon: 'none'
								})
							}
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			getCurrTime() {
				var current = new Date(); //实例化Date对象
				var m = current.getMonth() + 1; //获取月份，默认显示0-11，所以+1
				m = m < 10 ? ('0' + m) : m;
				var d = current.getDate(); //获取日期
				var h = current.getHours(); //获取时
				var mm = current.getMinutes(); //获取分
				// d = d < 10 ? ('0'+d) : d;
				h = h < 10 ? ('0' + h) : h;
				mm = mm < 10 ? ('0' + mm) : mm;
				this.textTime1 = h + ":" + mm;

				let weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
				this.textTime2 = m + "月" + d + "日" + " " + weekDays[current.getDay()];
			},
			getImageList() {
				let that = this;
				this.$api.request({
					url: this.$api.imageList,
					data: {
						type: 1
					},
					method: "POST",
				}).then(resp => {
					if (resp.code == 200) {
						that.imageIconAry = resp.data;
						that.imageIconAry.forEach((item, index) => {
							if (item.id == that.imageId) {
								that.swiperCurrent = index
							}
						})
					}
				});
			},
			// 轮播部分
			swiperChange(e) {
				this.swiperCurrent = e.detail.current;
			},
			getBannerList() {
				this.$api.request({
					url: this.$api.getBanner + "?location=5",
				}).then(res => {
					this.bannerList = res.data;
				})
			},
			// 轮播图跳转
			goSwiperDetail(item) {
				if (item.type === 1) {
					let str = JSON.stringify(item.content)
					uni.navigateTo({
						url: "/pages/detail/swiperDetail?bannerId=" + item.id
					})
				}
				if (item.type === 2) {
					uni.navigateTo({
						url: "/pages/index/webView?link=" + item.link
					})
				}
			},
		},
	};
</script>

<style lang="less" scoped>
	@import "@/static/css/app.less";

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #fff;
		padding: 20rpx;

		.swiper {
			position: relative;
			width: 100%;
			height: 1262rpx;

			.img1 {
				width: 100%;
				height: 100%;
			}
		}

		.image-left {
			position: fixed;
			top: 100rpx;
			left: 50rpx;
			height: 32rpx;
			width: 32rpx;
		}

		.text1 {
			position: absolute;
			top: 230rpx;
			font-size: 150rpx;
		}

		.text2 {
			position: absolute;
			top: 410rpx;
			font-size: 45rpx;
		}

		.wxad {
			position: relative;
			margin-top: 20rpx;
			width: 710rpx;
			border-radius: 10rpx;
			overflow: hidden;
		}


		.word {
			width: 700rpx;
			height: 40rpx;
			margin-top: 20rpx;
			color: #000;
			// background-color: #000;
		}

		.wallpapaer-class {
			margin-bottom: calc(124rpx + env(safe-area-inset-bottom));
			max-height: 2460rpx;
			overflow: hidden;
		}


		.bottom-view {
			position: fixed;
			bottom: 0;
			width: 100%;
			height: calc(124rpx + env(safe-area-inset-bottom));
			background: #ffffffd9;
			padding-bottom: env(safe-area-inset-bottom);
			display: flex;
			align-items: center;
			z-index: 999;

			.view-share {
				position: relative;
				margin-left: 50rpx;
				font-size: 20rpx;
				width: 100rpx;
				height: 100%;
				color: #000000;

				.share-button {
					width: 100%;
					height: 100%;
					position: absolute;
					opacity: 0;
				}

				img {
					width: 50rpx;
					height: 50rpx;
				}
			}

			.view-download {
				position: relative;
				margin-right: 40rpx;
				width: 370rpx;
				height: 90rpx;
				color: #000000;
				font-weight: 600;
				font-size: 36rpx;
				background: #ffbd16;
				border-radius: 90rpx;
			}
		}

		.ad {
			width: 710rpx;
			display: flex;
			position: relative;
			margin-top: 20rpx;
			height: 0;

			.ad-item {
				width: 100%;
				height: 100%;
				border-radius: 15rpx;

				.ad-img {
					height: 100% !important;
					width: 100%;
					border-radius: 15rpx;
				}
			}
		}
	}
</style>